package sms.service.impl;

import sms.model.Student;
import sms.persistence.IPersistenceDao;
import sms.persistence.PersistenceException;
import sms.service.IPersistenceDaoLoader;
import sms.service.IStudentService;
import sms.service.ServiceException;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

public class StudentServiceImpl implements IStudentService {
    private IPersistenceDao persistenceDao = IPersistenceDaoLoader.persistenceDao;

    @Override
    public List<Student> list() {
        try {
            return persistenceDao.list(Student.class);
        } catch (PersistenceException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public Optional<Student> get(Serializable id) {
        try {
            return persistenceDao.get(Student.class, id);
        } catch (PersistenceException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public String add(Serializable id, String name, String group) {
        try {
            return persistenceDao.save(new Student(String.valueOf(id), name, group));
        } catch (PersistenceException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public String update(Serializable id, String name, String group) {
        try {
            return persistenceDao.save(new Student(String.valueOf(id), name, group));
        } catch (PersistenceException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public String delete(Serializable id) {
        try {
            return persistenceDao.delete(Student.class, id);
        } catch (PersistenceException e) {
            throw new ServiceException(e);
        }
    }
}
